var Bullet = function() {
	// Conf
	this.speed = 5;
	this.bounds = {
		h:2,
		w:2
	};
	this.coords = {
		x:null,
		y:null,
		dx: 0,
		dy: 0 
	};

	this.source = null;

	this.init = function(source) {
		this.source = source;

		this.coords.dx = this.source.coords.dx;
		this.coords.x = this.source.coords.x + (this.source.bounds.w * this.coords.dx);
		this.coords.y = this.source.coords.y + this.source.bounds.h/3 + 3;

		return this;
	}

	this.render = function(ctx) {
		drawCircle(ctx, "FFFFFF", Math.abs(this.coords.x), this.coords.y, this.bounds.w);
	}

	this.update = function() {
		for (x in EnemyManager.entities) {
			var c = EnemyManager.entities[x];
			if (this.isCollide(c)) {
				this.destroy();
				EnemyManager.kill(x);
				break;
			}
		}

		if (this.isCollide(player)) {
			this.destroy();
			player.kill();
		}
		
		if(this.isInBounds(map.bounds) === true) {
			this.coords.x += this.speed * this.coords.dx;
		}
		else {
			this.destroy();
		}		
	}

	this.isInBounds = function(bounds) {
		var x = this.bounds.w/2 + this.coords.x + this.speed * this.coords.dx;
		if(x > 0 || Math.abs(x) >= bounds.w - this.bounds.w) {
			return false;
		}

		return true;
	}

	this.isCollide = function(e) {
		if (e == undefined )
			return false;

		if(typeof e.bounds.w === "undefined" || typeof e.bounds.h === "undefined")
			return false;

		return !(e.coords.x >= this.coords.x + this.bounds.w // Trop à droite
				|| e.coords.x + e.bounds.w <= this.coords.x // Trop à gauche
				|| e.coords.y >= this.coords.y + this.bounds.h // Trop en bas
				|| e.coords.y + e.bounds.h <= this.coords.y) // Trop en haut
	}

	this.destroy = function() {
		for (var i = 0, c = BulletManager.entities.length; i < c; i++) {
			if (BulletManager.entities[i] === this) {
				BulletManager.entities.splice(i, 1);
			}
		}
		
		return undefined;
	}
};